package com.luhuiguo.cms.server.config;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;

/**
 * The spring java-based configuration for JPA.
 * 
 * @author LuHuiguo
 * @version $Id: JpaConfig.java 174 2010-05-16 14:32:19Z luhuiguo@gmail.com $
 */
@Configuration
public class JpaConfig {

    public static final String PERSISTENCE_UNIT_NAME = "cms";

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        LocalEntityManagerFactoryBean factory = new LocalEntityManagerFactoryBean();
        factory.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
        factory.afterPropertiesSet();
        return factory.getObject();

    }
    
    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory());
        Map<String,Object> props= new HashMap<String,Object>();
        props.put("javax.persistence.FlushModeType", "COMMIT");
        transactionManager.setJpaPropertyMap(props);
        return transactionManager;

    }

//    @Bean
//    public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
//        return new PersistenceExceptionTranslationPostProcessor();
//    }

}
